import myCache from '../cache/my-cache.js'
export default {
  namespaced: true,
  state:() => ({
    address:JSON.parse(myCache.getItem('address') || '{}'),
    token:myCache.getItem('token') || '',
    userInfo:JSON.parse(myCache.getItem('userInfo') || '{}'),
    redirectInfo:null
  }),
  
  mutations:{
    //更新收货地址
    updateAddress(state,address){
      state.address = address
      
      this.commit("m_user/saveAddressToStorage")
    },
    
    //将地址存储到本地
    saveAddressToStorage(state){
      myCache.setItem('address',JSON.stringify(state.address))
    },
    
    //更新个人信息
    updateUserInfo(state,userInfo){
      state.userInfo = userInfo
      this.commit("m_user/saveUserInfoToStorage")
    },
    //将个人信息存储到本地
    saveUserInfoToStorage(state){
      myCache.setItem('userInfo',JSON.stringify(state.userInfo))
    },
    
    //更新token
    updateToken(state,token){
      state.token = token 
      this.commit("m_user/saveTokenToStorage")
      
    },
    //将token存储到本地
    saveTokenToStorage(state){
      myCache.setItem('token',state.token)
    },
    
    //更新导航信息
    updateRedirectInfo(state,info){
      state.redirectInfo = info
      
    },
    
  },
  
  getters:{
    //对地址进行拼接处理
    addressStr(state){
      if(!state.address.provinceName) return ''
      return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
    }
  }
}